package com.gdufe.product.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdufe.product.dto.PageProductDto;
import com.gdufe.product.dto.ProductDto;
import com.gdufe.product.dto.ProductStatusDto;
import com.gdufe.common.exception.ServiceException;
import com.gdufe.common.vo.PageBeanVo;
import com.gdufe.product.dao.ProductDao;
import com.gdufe.common.entity.Product;
import com.gdufe.product.service.ProductAdminService;
import com.gdufe.product.vo.ProductsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductAdminServiceImpl extends ServiceImpl<ProductDao, Product> implements ProductAdminService {

    @Autowired
    private ProductDao productDao;
    /**
     * 条件分页查询
     *
     * @param pageProductDto
     * @return
     */
    @Override
    public PageBeanVo<Product> pageQuery(PageProductDto pageProductDto) {
        //获取数据
        Long id = pageProductDto.getId();
        String name = pageProductDto.getName();
        Integer status = pageProductDto.getStatus();
        Integer page = pageProductDto.getPage();
        Integer pageSize = pageProductDto.getPageSize();

        //构建分页条件
        Page<Product> p = Page.of(page, pageSize);

        //构建查询条件
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<Product>()
                //精确匹配
                .eq(id != null, Product::getId, id)
                //模糊匹配
                .like(!StrUtil.isBlank(name), Product::getName, name)
                .eq(status != null, Product::getUpStatus, status);

        //查询
        page(p, wrapper);

        //封装数据返回
        return PageBeanVo.of(p.getTotal(), p.getRecords());
    }

    /**
     * 根据id修改商品状态
     *
     * @param productStatusDto
     */
    @Override
    public void updateStatus(ProductStatusDto productStatusDto) {
        //获取数据
        Long id = productStatusDto.getId();
        Integer upStatus = productStatusDto.getUpStatus();

        //封装数据
        Product product = new Product();
        product.setId(id);
        product.setUpStatus(upStatus);

        //更新数据
        updateById(product);
    }


    /**
     * 新增商品
     *
     * @param productDto
     */
    @Override
    public void insert(ProductDto productDto) {
        Product product = new Product();
        BeanUtil.copyProperties(productDto, product);
        save(product);
    }

    /**
     * 根据id删除商品
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        Product product = getById(id);

        //判断商品是否处于上架状态，如果是则不能删除
        if (product.getUpStatus() == 1) {
            throw new ServiceException(500, "商品上架中，不能删除");
        }

        //删除数据
        removeById(id);
    }

    /**
     * 批量删除商品
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 修改商品信息
     *
     * @param productDto
     */
    @Override
    public void alter(ProductDto productDto) {
        Product product = new Product();
        BeanUtil.copyProperties(productDto, product);
        updateById(product);
    }

    @Override
    public List<ProductsVo> getPros() {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.select("id","name").groupBy("id");
        List<ProductsVo> products = productDao.selectList(wrapper).stream().map(item->{
            ProductsVo vo = new ProductsVo();
            vo.setId(item.getId());
            vo.setName(item.getName());
            return vo;
        }).collect(Collectors.toList());
        return products;

    }
}
